package com.xmy.cultivate.controller;

import com.alibaba.fastjson.JSONObject;
import com.xmy.cultivate.bean.message.template.TemplateDate;
import com.xmy.cultivate.bean.message.template.TemplateMessage;
import com.xmy.cultivate.entity.Course;
import com.xmy.cultivate.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 发送模板消息
 */
@RestController
@RequestMapping("/api/wecharTemp")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @RequestMapping("/setIndustry")
    public String setIndustry(@RequestBody String body) throws IOException {
        return templateService.setIndustry(body);
    }

    @RequestMapping("/getIndustry")
    public String setIndustry() throws IOException {
        return templateService.getIndustry();
    }

    @RequestMapping("/getTemplateList")
    public String getTemplateList() throws IOException {
        return templateService.getTemplateList();
    }

    @RequestMapping("/sendSignIn")
    public String sendSignIn(@RequestBody String body){
        //System.out.println("obj:"+body);
        return body;
    }

    @RequestMapping("/sendWecharMsg")
    public String sendWecharMsg(@RequestBody String body) throws IOException {

        //System.out.println("=================");
        //System.out.println(body);
        return templateService.send(body);
    }

    @RequestMapping("/delMenu")
    public String delMenu(@RequestBody String body) throws IOException {
        return templateService.delMenu(body);
    }

    @RequestMapping("/createMenuZnb")
    public String createMenu(@RequestBody String body) throws IOException {
        if (body == null || body.equals("")) {
            body = "{\"button\":[{\"type\":\"view\",\"name\":\"课程表\",\"url\":\"http://parent.5ixmy.xyz/timetable\"}]}";
        }
        return templateService.createMenu(body);
    }

    @RequestMapping("/send")
    public String send(@RequestBody String body) throws IOException {

        String touser="oWsxs5vAlXuEE5V7X2aLXJS89jwk";

        String template_id="4nME5oxsV5XW9tTpPixMgDQKyv6zi6uJ__Mnw9gTcKc";

        String url="";

        TemplateDate templateDate = TemplateDate.builder().first("您好，您的小孩进行了刷卡操作。").keyword1("张三").keyword2("2018-09-04 11:56:54").remark("击可查看详细信息（照片，学生信息），感谢您的支持，谢谢！").build();

        TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id("1237").data(templateDate).build();

        String obj = templateMessage.toJson();

        //System.out.println("obj:"+obj);


        return templateService.send(obj);
    }


    @RequestMapping(value = "/test",method=RequestMethod.POST)
    public String Test(@RequestBody Course course){
        //System.out.println("course");
        //System.out.println(course);
        return "调用成功";
    }
}
